package com.photoeditor.demo.ui.widget.image.emoji;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.imageloader.GlideApp;
import com.photoeditor.demo.model.bean.EmojiBean;
import com.photoeditor.demo.model.image.emoji.LocalStickerData;

/**
 * emoji单个ITEM 的View
 */
public class EmojiItem extends RelativeLayout {
    private EmojiBean mItemData;
    public ImageView mIcon;

    public EmojiItem(Context context, int width, int height) {
        super(context);
        initView(width, height);
    }

    private void initView(int width, int height) {
        LayoutInflater.from(getContext()).inflate(R.layout.item_emoji, this, true);
        setBackgroundResource(R.drawable.emoji_item_selector);
        mIcon = (ImageView) findViewById(R.id.emoji);
        LayoutParams lp = (LayoutParams) mIcon.getLayoutParams();
        lp.width = width;
        lp.height = height;
        mIcon.setLayoutParams(lp);
    }

    public EmojiBean getItemData() {
        return mItemData;
    }

    /**
     * @param item 数据体
     */
    public void setItemData(EmojiBean item) {
        this.mItemData = item;
        if (item.isType(EmojiBean.TYPE_ONLINE) && !TextUtils.isEmpty(item.getIconUrl())) {
            try {
                GlideApp.with(ComponentContext.getContext())
                        .load(item.getIconUrl())
                        .placeholder(R.drawable.ic_sticker_default_72)
                        .error(R.drawable.ic_default_image)
                        .into(mIcon);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            try {
                LocalStickerData.getInstance().loadEmojiThumbnail(this, item);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setIconBitmap(Bitmap b) {
        mIcon.setImageBitmap(b);
    }
}
